V_t(TString file){

  cout << file << endl;

  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(0);

  TFile *f = new TFile(file);

  cout << endl;
  cout << "remeber to set by hand the channel ch?" << endl;
  cout << endl;

  TGraph *gr_vt = (TGraph*)f->Get("gr_vt_ch3");
  TGraph *gr_vt_sel0 = (TGraph*)f->Get("gr_vt_sel0_ch3");
  TGraph *gr_vt_sel1 = (TGraph*)f->Get("gr_vt_sel1_ch3");
  TGraph *gr_vt_sel2 = (TGraph*)f->Get("gr_vt_sel2_ch3");

  TGraph *gr_vt_ref = (TGraph*)f->Get("gr_vt_ref");
  TGraph *gr_vt_ref_dt_al = (TGraph*)f->Get("gr_vt_ref_dt_al");
  TH1D *hV_sgn_t = (TH1D*)f->Get("hV_sgn3_t");
  TH1D *hV_ref_sgn_t = (TH1D*)f->Get("hV_ref_sgn3_t");

  TH1D *hV_t = (TH1D*)hV_sgn_t->Clone("hV_t");

  TH1D *hV_sgn_vrms_t = (TH1D*)f->Get("hV_sgn3_vrms_t");

  TH1D *h_dt = new TH1D("h_dt","h_dt",80,-1.,1.);
  double dt_al;
  TTree *fTreeOut = (TTree*)f->Get("fTreeOut");
  fTreeOut->SetBranchAddress("dt_al",&dt_al);
  for (int k=0; k<fTreeOut->GetEntries(); k++) { 
    fTreeOut->GetEntry(k);
    double dt = 0.025;    
    h_dt->Fill(dt_al*1e9+dt/10.);
  }


  char sdname1[100],sdname2[100];
  sprintf(sdname1,"time [ns]");
  sprintf(sdname2,"voltage [mV]");

  TCanvas *c0 = new TCanvas("c0","time windows 1 and 2 (blue) to make the spectrum", 950,850);
  SetTCanvasStyle(c0);
  SetTGraphStyle(gr_vt,sdname1,sdname2,1,1,0);
  SetTGraphStyle(gr_vt_sel0,sdname1,sdname2,1,2,0);
  SetTGraphStyle(gr_vt_sel1,sdname1,sdname2,1,4,0);
  SetTGraphStyle(gr_vt_sel2,sdname1,sdname2,1,4,0);
  gr_vt->Draw("AL");
  gr_vt_sel0->Draw("LSAME");
  gr_vt_sel1->Draw("LSAME");
  gr_vt_sel2->Draw("LSAME");
  c0->Print("Spectrum_time_windows.pdf");

  TCanvas *c1a = new TCanvas("c1a","time shift distribution", 950,850);
  SetTCanvasStyle(c1a);
  SetTH1DStyle(h_dt,"#Deltat [ns]","entries",1,1,0,0);
  h_dt->Draw();
  cout << endl;
  cout << "alignment performance" << endl;
  cout << "mean = " << h_dt->GetMean()*1e3 << " +/- " << h_dt->GetMeanError()*1e3 << " ps " << endl;  
  cout << "RMS  = " << h_dt->GetRMS()*1e3 << " ps " << endl;
  cout << endl;
  //h_dt->Fit("gaus");
  c1a->Print("dt.pdf");

  TCanvas *c1 = new TCanvas("c1","time window (blue) to make the alignment", 950,850);
  SetTCanvasStyle(c1);
  SetTGraphStyle(gr_vt_ref,sdname1,sdname2,1,1,0);
  SetTGraphStyle(gr_vt_ref_dt_al,sdname1,sdname2,1,4,0);
  gr_vt_ref->Draw("AL");
  gr_vt_ref_dt_al->Draw("LSAME");
  c1->Print("Alignment_time_window.pdf");

  TCanvas *c2 = new TCanvas("c2","Alignment performance", 950,850);
  SetTCanvasStyle(c2);
  double x1,x2,y;
  gr_vt_ref->GetPoint(0               ,x1,y);
  gr_vt_ref->GetPoint(gr_vt_ref->GetN()-1,x2,y);
  hV_sgn_t->GetXaxis()->SetRangeUser(x1,x2);
  hV_ref_sgn_t->GetXaxis()->SetRangeUser(x1,x2);
  SetTH1DStyle(hV_ref_sgn_t,sdname1,sdname2,2,30,0,0);
  SetTH1DStyle(hV_sgn_t,sdname1,sdname2,1,1,0,0);
  hV_ref_sgn_t->Draw("L");
  hV_sgn_t->Draw("LSAME");
  c2->Print("Alignment_performance.pdf");

  double t1range_shift = 8.;
  double t2range_shift = -2.;


  TCanvas *c3 = new TCanvas("c3","Average signal", 950,850);
  SetTCanvasStyle(c3);
  SetTH1DStyle(hV_t,sdname1,sdname2,1,1,0,0);
  hV_t->GetXaxis()->SetRangeUser(x1+t1range_shift,x2+t2range_shift);
  hV_t->Draw("L");
  c3->Print("AverageSignal.pdf");

  TCanvas *c4 = new TCanvas("c4","VRMS vs time", 950,850);
  SetTCanvasStyle(c4);
  sprintf(sdname2,"V_{RMS} [mV]");
  SetTH1DStyle(hV_sgn_vrms_t,sdname1,sdname2,1,1,0,0);
  hV_sgn_vrms_t->GetXaxis()->SetRangeUser(x1+t1range_shift,x2+t2range_shift);
  hV_sgn_vrms_t->Draw("L");
  c4->Print("VRMS_t.pdf");


}

void SetTCanvasStyle(TCanvas *ccanvas){
  ccanvas->SetLeftMargin(0.120);
  ccanvas->SetBottomMargin(0.120);
}

void SetTGraphStyle(TGraph *gre, char XTitle[150], char YTitle[150], int marker_style, int marker_color, double marker_size){
  gre->SetTitle();
  gre->SetMarkerStyle(marker_style);
  gre->SetMarkerColor(marker_color);
  gre->SetMarkerSize(marker_size);
  int line_color = marker_color;
  gre->SetLineColor(line_color);
  gre->SetLineWidth(1);
  gre->GetXaxis()->SetLabelFont(132);
  gre->GetYaxis()->SetLabelFont(132);
  gre->GetXaxis()->SetLabelSize(0.045);
  gre->GetYaxis()->SetLabelSize(0.045);
  gre->GetXaxis()->SetTitleFont(132);
  gre->GetYaxis()->SetTitleFont(132);
  gre->GetXaxis()->SetTitleSize(0.06);
  gre->GetYaxis()->SetTitleSize(0.06);
  gre->GetXaxis()->SetTitleOffset(0.9);
  gre->GetYaxis()->SetTitleOffset(0.85);
  gre->GetXaxis()->SetTitle(XTitle);
  gre->GetYaxis()->SetTitle(YTitle);
}


void SetTH1DStyle(TH1D *h1, char XTitle[150], char YTitle[150], int line_width, int line_color, int fill_style, int fill_color){
  h1->SetTitle();
  h1->SetLineWidth(line_width);
  h1->SetLineColor(line_color);
  h1->SetFillStyle(fill_style);
  h1->SetFillColor(fill_color);
  h1->GetXaxis()->SetLabelFont(132);
  h1->GetYaxis()->SetLabelFont(132);
  h1->GetXaxis()->SetLabelSize(0.045);
  h1->GetYaxis()->SetLabelSize(0.045);
  h1->GetXaxis()->SetTitleFont(132);
  h1->GetYaxis()->SetTitleFont(132);
  h1->GetXaxis()->SetTitleSize(0.06);
  h1->GetYaxis()->SetTitleSize(0.06);
  h1->GetXaxis()->SetTitleOffset(0.9);
  h1->GetYaxis()->SetTitleOffset(0.85);
  h1->GetXaxis()->SetTitle(XTitle);
  h1->GetYaxis()->SetTitle(YTitle);
}

